package com.zian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zian.context.BaseContext;
import com.zian.domain.dto.IndexConfigDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.po.SysGoodsInfo;
import com.zian.domain.po.SysIndexConfig;
import com.zian.domain.query.IndexConfigQuery;
import com.zian.domain.query.PageQuery;
import com.zian.domain.vo.IndexConfigQueryVO;
import com.zian.domain.vo.IndexConfigVO;
import com.zian.enums.IsDeletedEnum;
import com.zian.exception.BadRequestException;
import com.zian.exception.DbException;
import com.zian.mapper.SysIndexConfigMapper;
import com.zian.service.ISysGoodsInfoService;
import com.zian.service.ISysIndexConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Service
@RequiredArgsConstructor
public class SysIndexConfigServiceImpl extends ServiceImpl<SysIndexConfigMapper, SysIndexConfig> implements ISysIndexConfigService {

    private final ISysGoodsInfoService goodsInfoService;


    @Override
    public List<IndexConfigVO> getIndexConfigList(Integer type) {
        PageQuery query = new PageQuery();
        query.setPageNo(1);
        query.setPageSize(10);
        query.toMpPage(OrderItem.desc("config_rank"));
        List<IndexConfigVO> vos = getBaseMapper().getIndexConfigList(query, type);
        if (CollUtil.isEmpty(vos)) return CollUtil.empty(List.class);
        return vos;
    }

    @Override
    public PageDTO<IndexConfigQueryVO> queryIndexConfigList(IndexConfigQuery query) {
        Page<SysIndexConfig> page = query.toMpPage(OrderItem.desc("config_rank"));
        lambdaQuery()
                .eq(SysIndexConfig::getConfigType, query.getConfigType())
                .page(page);
        List<SysIndexConfig> records = page.getRecords();
        if (CollUtil.isEmpty(records)) return PageDTO.empty(0L, 0L);
        return PageDTO.of(page, IndexConfigQueryVO.class);
    }

    @Override
    @Transactional
    public void deleteIndexConfigByIds(List<Long> ids) {
        Long currentId = BaseContext.getCurrentId();
        if (CollUtil.isEmpty(ids)) throw new BadRequestException("请选择要删除的配置");
        boolean b = lambdaUpdate()
                .in(SysIndexConfig::getId, ids)
                .set(SysIndexConfig::getIsDeleted, 1)
                .set(SysIndexConfig::getUpdateUser, currentId)
                .set(SysIndexConfig::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) throw new DbException("删除失败");
    }

    @Override
    public void saveIndexConfig(IndexConfigDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        SysIndexConfig one = lambdaQuery().eq(SysIndexConfig::getGoodsId, dto.getGoodsId()).one();
        if (one != null) throw new BadRequestException("配置项重复");
        SysGoodsInfo goodsInfo = goodsInfoService.getById(dto.getGoodsId());
        if (goodsInfo == null) throw new BadRequestException("商品不存在");
        SysIndexConfig indexConfig = BeanUtil.toBean(dto, SysIndexConfig.class);
        indexConfig.setIsDeleted(IsDeletedEnum.NO);
        indexConfig.setCreateUser(currentId);
        indexConfig.setUpdateUser(currentId);
        boolean b = save(indexConfig);
        if (!b) throw new DbException("添加失败");
    }

    @Override
    public void updateIndexConfig(IndexConfigDTO dto) {
        Long currentId = BaseContext.getCurrentId();

        // 校验
        SysIndexConfig dbConfig = getById(dto.getId());
        if (dbConfig == null) throw new BadRequestException("配置不存在");
        SysIndexConfig one = lambdaQuery()
                .eq(SysIndexConfig::getConfigType, dto.getConfigType())
                .eq(SysIndexConfig::getGoodsId, dto.getGoodsId())
                .one();
        if (one != null && !dbConfig.getId().equals(one.getId())) throw new BadRequestException("配置项重复");

        SysGoodsInfo goodsInfo = goodsInfoService.getById(dto.getGoodsId());
        if (goodsInfo == null) throw new BadRequestException("商品不存在");

        // 更新
        dbConfig.setConfigType(dto.getConfigType());
        dbConfig.setConfigName(dto.getConfigName());
        dbConfig.setConfigRank(dto.getConfigRank());
        dbConfig.setRedirectUrl(dto.getRedirectUrl());
        dbConfig.setGoodsId(dto.getGoodsId());
        dbConfig.setUpdateUser(currentId);
        boolean b = updateById(dbConfig);
        if (!b) throw new DbException("修改失败");
    }
}
